<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" ref="filters" :size="size">
        <el-form-item prop="apply_type">
          <el-select v-model="filters.apply_type">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in apply_type"
              :key="item.key"
              :label="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="reciever">
          <el-input
            v-model="filters.reciever"
            placeholder="收款人姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="filters.status">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.val"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-search"
            :label="$t('action.search')"
            type="primary"
            @click="findPage(null)"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            icon="fa fa-refresh" :label="$t('action.reset')"
            type="primary"
            @click="resetForm('filters')"
          />
        </el-form-item>
        <el-form-item>
          <kt-button label="新增申请" type="primary" @click="handleAdd" />
        </el-form-item>
        <el-form-item>
          <kt-button label="提交审核" type="primary" @click="setStaus()" />
        </el-form-item>
      </el-form>
    </div>
    <div
      class="toolbar"
      style="float:right;padding-top:10px;padding-right:15px;"
    >
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button
                icon="fa fa-refresh"
                @click="findPage(null)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button
                icon="fa fa-filter"
                @click="displayFilterColumnsDialog"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button
                icon="fa fa-file-excel-o"
                @click="downloadExcel"
                id="downloadExcel"
              ></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      :showBatchDelete="true"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>

    <!--新增编辑界面-->
    <el-dialog
      :title="operation ? '新增' : '编辑'"
      width="60%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        :inline="true"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input
            v-model="dataForm.id"
            :disabled="true"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属门店" prop="shopname">
          <template>
            <el-autocomplete
              :fetch-suggestions="queryShopInfo"
              placeholder="请输入内容"
              @select="handleSelectShopInfo"
              v-model="dataForm.shopname"
              style="width:200px"
            ></el-autocomplete>
          </template>
        </el-form-item>
        <el-form-item label="申请金额" prop="apply_num">
          <el-input v-model="dataForm.apply_num" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="账号方式" prop="apply_type">
          <el-select v-model="dataForm.apply_type" style="width: 214.5px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in apply_type"
              :key="item.key"
              :label="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收款人账号" prop="recievea_ccount">
          <el-input
            v-model="dataForm.recievea_ccount"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="收款人姓名" prop="reciever">
          <el-input v-model="dataForm.reciever" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="申请时间" prop="apply_time">
          <el-date-picker
            v-model="dataForm.apply_time"
            type="datetime"
            placeholder="选择日期时间"
            style="width: 214.5px"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
          <!-- <el-input v-model="dataForm.apply_time" auto-complete="off"></el-input> -->
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="dataForm.status" style="width: 214.5px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.val"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{
          $t("action.cancel")
        }}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
          >{{ $t("action.submit") }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import utils from "@/assets/js/utils.js";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      list: [],
      shoplist: [],
      status: [
        { key: "0", value: "审核通过" },
        { key: "1", value: "审核拒绝" }
      ],
      apply_type: [
        { key: "0", value: "微信" },
        { key: "1", value: "支付宝" },
        { key: "2", value: "银行卡" }
      ],
      filters: {
        reciever: "",
        status: "",
        apply_type: "",
        t: "applyCash"
      },
      columns: [],
      checked: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        apply_num: [
          { required: true, message: "请输入申请金额", trigger: "blur" }
        ],
        apply_type: [
          { required: true, message: "请选择账号方式", trigger: "blur" }
        ],
        recievea_ccount: [
          { required: true, message: "请输入收款人帐号", trigger: "blur" }
        ],
        apply_time: [
          { required: true, message: "请输入收款人账号", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请输入申请时间", trigger: "blur" }
        ],
        reciever: [
          { required: true, message: "请选择收款人", trigger: "blur" }
        ],
        shopname: [
          { required: true, message: "请输入店铺名称", trigger: "blur" }
        ]
      },

      // 新增编辑界面数据
      dataForm: {
        shop_id: ""
      }
    };
  },

  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },
    // 提现状态初始化
    queryUserList() {
      var that = this;
      let param = {};
      param.t = "sysConfig";
      param.type = "apply_cash_status";
      this.utils.request.queryUserList(param, function(data) {
        that.list = data.data;
      });
    },

    //门店状态
    queryShopInfo(queryString, cb) {
      if (queryString !== "" && queryString != undefined) {
        //查询后台客户信息
        var search = {};
        search.t = "shop";
        search.name = queryString;
        var this_ = this;
        this.utils.request.queryUserList(search, function(res) {
          var restaurants = [];
          if (res.data != null) {
            restaurants = res.data;
          } else {
            this_.$message({ message: "请输入正确的门店", type: "error" });
            this_.dataForm.shopname = "";
            return;
          }

          var result = [];
          $.each(restaurants, function(key, val) {
            result.push(val);
          });
          cb(result);
        });
      } else {
        cb([]);
      }
    },
    handleSelectShopInfo(item) {
      if (item != null && item != "") {
        var this_ = this;
        if (item != null && item != "") {
          this_.dataForm.shop_id = item.id;
        } else {
        }
      }
    },

    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "提现申请";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
    },

    // 显示编辑界面
    handleEdit: function(params) {
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 批量删除
    handleDelete: function(data) {
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      data.t = "applyCash";
      data.ids = ids;
      this.utils.request.deleteUserInfo(data, data.callback);
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "shopname", label: "所属门店", minWidth: 120 },
        { prop: "username", label: "申请人", minWidth: 120 },
        { prop: "apply_num", label: "申请金额", minWidth: 120 },
        { prop: "apply_type", label: "放款方式", minWidth: 150 },
        { prop: "reciever", label: "收款人姓名", minWidth: 120 },
        { prop: "recievea_ccount", label: "收款人帐号", minWidth: 120 },
        { prop: "apply_time", label: "申请时间", minWidth: 120 },
        { prop: "status_alias", label: "状态", minWidth: 120 }
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.findPage();
    },

    //选择框
    selectionChange: function(params) {
      this.checked = params.selections;
    },
    //提交审核
    setStaus() {
      if (this.checked.length == 1 && this.checked[0].status == "120") {
        let params = {};
        let this_ = this;
        params.t = "applyCash";
        params.id = this.checked[0].id;
        params.status = "119";
        this.utils.request.editUserInfo(params, function(data) {
          if (data.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage();
          } else {
            this_.$message({ message: "操作失败, " + data.msg, type: "error" });
          }
        });
      } else {
        this.$message({
          message: "提交审核只能选择一条待审核数据",
          type: "error"
        });
      }
    },
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.t = "applyCash";
            this.utils.request.editUserInfo(params, this.editInfoBack);
          });
        }
      });
    },
    // 新增修改回调
    editInfoBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
        this.dialogVisible = false;
        this.$refs["dataForm"].resetFields();
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage();
      this.dialogVisible = false;
      this.operation = false;
      this.editLoading = false;
    }
  },
  mounted() {
    this.initColumns();
    this.queryUserList();
  }
};
</script>

<style scoped>
/deep/ .el-input__inner   {
  width: 200px !important;
}
</style>